//
//  TabItemView.swift
//  WWDCLearn
//
//  Created by mac on 2021/6/30.
//

import Cocoa
import ConfUIFoundation

extension NSStackView {
    var computedContentSize: CGSize {
        func horizontal(

        ) -> CGSize {
            let height = arrangedSubviews.map( { $0.bounds.height}).max() ?? CGFloat(0)
            let width = arrangedSubviews.reduce(CGFloat(0), {$0 + $1.intrinsicContentSize.width + spacing})
            return CGSize(width: width - spacing, height: height)
        }

        switch orientation {
        case .horizontal:
               return horizontal()
        case .vertical:
            let width = arrangedSubviews.map({ $0.bounds.width}).max() ?? CGFloat(0)
            let height = arrangedSubviews.reduce(CGFloat(0), {$0 + $1.intrinsicContentSize.height + spacing })
            return CGSize(width: width, height: height - spacing)
        }
    }
}


final class TabItemView:NSView {
  @objc  var target:Any?
    @objc   var action:Selector?
    var controllerIdentifier:String = ""
    
    lazy var titleLabel: NSTextField = {
        let l = NSTextField(labelWithString: "")
        l.font =  .systemFont(ofSize: 14)
        l.textColor = .toolbarTint
        l.cell?.backgroundStyle = .dark
        return l
    }()
    
    var title: String? {
        didSet{
            titleLabel.stringValue = title ?? ""
            titleLabel.sizeToFit()
            sizeToFit()
        }
    }

    var image: NSImage? {
        didSet {
            if state == .off {
                imageView.image = image
                sizeToFit()
            }
        }
    }

    var alternateImage: NSImage? {
        didSet {
            if state == .on {
                imageView.image = alternateImage
                sizeToFit()
            }
        }
    }

   lazy var imageView: MaskImageView = {
    let v = MaskImageView()
    v.tintColor = .toolbarTint
    v.widthAnchor.constraint(equalToConstant: 20).isActive = true
    v.heightAnchor.constraint(equalToConstant: 15).isActive = true
     return v
    }()


    lazy var stackView: NSStackView = {
        let v = NSStackView(views: [self.imageView,self.titleLabel])
        v.orientation = .horizontal
        v.spacing = 10
        v.alignment = .centerY
        v.distribution = .equalCentering
        return v
    }()

  override  var intrinsicContentSize: NSSize {
    get {
        var s = stackView.computedContentSize
        s.width += 29
        return s
    }
    set {

    }
    }

    private var uiMaskNotificationTokens: [NSObjectProtocol] = []

  override  init(frame frameRect:NSRect){
    super.init(frame: frameRect)
    wantsLayer = true
    addSubview(stackView)
    stackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
    stackView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true

    titleLabel.centerYAnchor.constraint(equalTo: stackView.centerYAnchor, constant: -1).isActive = true

    let showToken = NotificationCenter.default.addObserver(forName: .WWDCWindowWillShowUIMask, object: nil, queue: .main) { [weak self] _ in
        self?.isEnabled = false
    }
    let hideToken = NotificationCenter.default.addObserver(forName: .WWDCWindowWillHideUIMask, object: nil, queue: .main) { [weak self] _ in
        self?.isEnabled = true
    }

    uiMaskNotificationTokens = [showToken, hideToken]
    }
    var isEnabled = true {
        didSet {
            animator().alphaValue = isEnabled ? 1 : 0.3
        }
    }



    override var mouseDownCanMoveWindow: Bool { false }


    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func sizeToFit()  {
        frame = NSRect(x: frame.origin.x, y: frame.origin.y, width: intrinsicContentSize.width, height: intrinsicContentSize.height)

    }

    var state: NSControl.StateValue = .off {
        didSet {
            if state == .on {

            }else {

            }
        }
    }
    
}
